package gem.model.course;

import gem.database.Common;

import java.util.Vector;

public class ScienceDomain
{
	private Department m_Department = null;
	private java.util.Vector<Course> m_Courses = null;
	//====================================================================================//
	private String m_ScienceDomainTitle;
	private int m_ScienceDomainID;
	private boolean m_IsModified;
	//====================================================================================//
	// ScienceDomain WILL NOT set the Department parent! Department who holds this science domain
	// will set itself as parent!
	public ScienceDomain( String name, int id, Department d )
	{
		m_ScienceDomainID = id;
		m_ScienceDomainTitle = name;
		
		d.addScienceDomain( this ); // This will also set parent!
		
		this.m_Courses = new Vector< Course >();
		
		setModified( false );
	}
	//====================================================================================//
	public ScienceDomain( String name, Department d )
	{
		m_ScienceDomainTitle = name;
		m_ScienceDomainID = Common.DB_NEW_ENTRY;
		
		m_Courses = new Vector<Course>();
		
		setModified( false );
		
		d.addScienceDomain( this ); // This will also set parent!
	}
	//====================================================================================//
	public void addCourse( Course c )
	{
		if( m_Courses.contains( c ) )
		{
			return;
		}
		ScienceDomain prevSD = c.getScienceDomain();
		
		if( prevSD != null )
		{
			prevSD.removeCourse( c );
			
			Association.scienceDomainChanged( c, prevSD, this );
		}
		c.setScienceDomain( this );
		
		m_Courses.add( c );
		
		setModified( true );
	}
	//====================================================================================//
	public void removeCourse( Course c )
	{
		m_Courses.remove( c );
	}
	//====================================================================================//
	//====================================================================================//
	public void setModified( boolean mod ){ this.m_IsModified = mod; }
	//====================================================================================//
	public Course getCourse( String name )
	{
		for( Course c : m_Courses )
		{
			if( c.getTitle().equals(name))
			{
				return c;
			}
		}
		return null;
	}
	//====================================================================================//
	public Course getCourse( int id )
	{
		for( Course c : m_Courses )
		{
			if( c.getId() == id )
			{
				return c;
			}
		}
		return null;
	}
	//====================================================================================//
	public java.util.Vector<Course>getCourses()
	{
		return m_Courses;
	}
	//====================================================================================//
	public String getTitle() { return m_ScienceDomainTitle; }
	public int getId() { return m_ScienceDomainID; }
	public void setId( int id ) { this.m_ScienceDomainID = id; }
	//====================================================================================//
	public void setCourses( java.util.Vector<Course> c )
	{
		m_Courses = c;
		setModified( false );
	}
	//====================================================================================//
	public void setDepartment( Department newParent )
	{
		this.m_Department = newParent;
	}
	//====================================================================================//
	public Department getDepartment() { return m_Department; }
	//====================================================================================//
	public void setTitle( String title )
	{
		m_ScienceDomainTitle = title;
		m_IsModified = true;
	}
	//====================================================================================//
	public boolean getIsModified() { return m_IsModified; }
	public void setIsModified( boolean modified ) { m_IsModified = modified; }
	//====================================================================================//
	@Override
	public String toString()
	{
		return this.m_ScienceDomainTitle;
	}
	//====================================================================================//
	@Override
	public boolean equals(Object obj)
	{
		if( obj instanceof ScienceDomain == false )
		{
			return false;
		}
		ScienceDomain temp = ((ScienceDomain)obj);
		return ( temp.getId() == this.getId()) && (temp.getTitle().equals(this.getTitle()));
	}
	//====================================================================================//
	@Override
	public int hashCode()
	{
		return getTitle().hashCode();
	}
	//====================================================================================//

}
